package com.lzx.adx.report.support.strategy.v3.channel;

import cn.hutool.http.HttpUtil;
import com.lzx.adx.report.annnotions.ProductType;
import com.lzx.adx.report.constant.ProductChannelConstant;
import com.lzx.adx.report.dto.OcpxAccountDTO;
import com.lzx.adx.report.dto.ProductConfigDTO;
import com.lzx.adx.report.dto.ReportDTO;
import com.lzx.adx.report.util.URLEncoderDecoderTool;
import com.lzx.adx.report.util.UrlUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;


@Component
@Slf4j
@ProductType(ProductChannelConstant.SFKJ)
public class SfkjReport extends AbstractProductReport {
    @Override
    public String execute(ReportDTO reportDTO, OcpxAccountDTO ocpxAccountDTO, ProductConfigDTO productConfigDTO) {
        Map<String,Object> params=new HashMap<>();
        if(StringUtils.isNotBlank(reportDTO.getImei()) &&!StringUtils.contains(reportDTO.getImei(),"__")){
            params.put("imei", reportDTO.getImei());
        }
        if(StringUtils.isNotBlank(reportDTO.getImeiSum()) &&!StringUtils.contains(reportDTO.getImeiSum(),"__")){
            params.put("imeiSum", reportDTO.getImeiSum());
        }
        if(StringUtils.isNotBlank(reportDTO.getOaid()) &&!StringUtils.contains(reportDTO.getOaid(),"__")){
            params.put("oaid", reportDTO.getOaid());
        }
        if(StringUtils.isNotBlank(reportDTO.getOaidSum()) &&!StringUtils.contains(reportDTO.getOaidSum(),"__")){
            params.put("oaidSum", reportDTO.getOaidSum());
        }
        String format = String.format(productConfigDTO.getCallbackUrl(), reportDTO.getBizRequestId());
        params.put("callbackUrl",format);
        params.put("ip",reportDTO.getIp());
        params.put("time",System.currentTimeMillis());
        params.put("os",1);
        params.put("ua", URLEncoderDecoderTool.encodeURL(reportDTO.getUser_agent()));
        String urlParams = UrlUtil.covertParamsOrderByAsc(params);

        String finalUrl = ocpxAccountDTO.getReportUrl() + "&" + urlParams;
        log.info("账号为{},请求产品端的完整地址为 {}", ocpxAccountDTO.getId(), finalUrl);
        String body = HttpUtil.createGet(finalUrl).execute().body();
        log.info("账号为{} 请求产品端，响应的结果为{}", ocpxAccountDTO.getId(), body);

        ocpxAccountDTO.setFinalURL(finalUrl);
        return body;
    }
}
